package com.example.demo2.test.service;

import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONObject;
import com.example.demo2.test.websocket.BinanceWebSocket;
import com.example.demo2.test.model.MarketEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;

/**
 * Description:
 *
 * @author my.miao
 * @date 2022/1/5 15:03
 */
@Slf4j
@Component
public class BinanceService {
//    @Resource
//    private SwapMarketMapper marketMapper;

//    @Resource
//    private TradeService tradeService;
    @Resource
    private ExecutorService executorService;

    private static String DEPTH = "depth";
    private static String DETAIL = "detail";
    private static String MARK_PRICE = "mark-price";
    private static Map<String,String> symbolMap = new HashMap<>();// 对应请求数据的交易对
    private static Map<String,String> subTypeMap = new HashMap<>();// 对应请求数据的类型

    @Value("${binance.socket.api}")
    String api;

    BinanceWebSocket webSocket;

    public void message(String text) {
        log.info("收到消息了" + text);
    }

    public void binanceMarket() {
        List<MarketEntity> list = new ArrayList<>();// 查询要采集的交易对
//        list.add(new MarketEntity("vet_usdt", "binance01",0,1 ));
        list.add(new MarketEntity("btc_usdt", "binance02",0,1 ));

        webSocket = BinanceWebSocket.createConnectionManage(this,api);// 创建websocket连接
        List<String> subDepthList = new ArrayList<>();
        List<String> subTradeList = new ArrayList<>();
        for(MarketEntity item : list) {
            String symbol = item.getSymbol().replace("_","").toLowerCase();// 调整交易对
//            String subDepthStr = symbol+"@depth5";// 订阅盘口
//            String subMinTicker = symbol+"!miniTicker@arr";// 精简Ticker
//            String subMinTicker = symbol+"@ticker";// 完整Ticker
            String subDepthTicker = symbol+"@depth5@100ms";// 完整Ticker
            String subTradeStr = symbol+"@aggTrade";// 订阅成交记录
            subDepthList.add(subDepthTicker);
            subDepthList.add(subTradeStr);
//            subDepthList.add(subMinTicker);
//            symbolMap.put(subDepthStr,item.getSymbol());
//            subTypeMap.put(subDepthStr,DEPTH);
            if(item.getTradeLog().equals(1)){// 判断是否订阅成交记录
//                String subTradeStr = symbol+"@aggTrade";// 订阅成交记录
                String subMarkPriceStr = symbol+"@markPrice@1s";// 订阅最新标记价格

                subTradeList.add(subTradeStr);
                subTradeList.add(subMarkPriceStr);
//                subTradeList.add(subMinTicker);
//                subTradeList.add(subDepth);
                symbolMap.put(subTradeStr,item.getSymbol());
                subTypeMap.put(subTradeStr,DETAIL);
                symbolMap.put(subMarkPriceStr,item.getSymbol());
                subTypeMap.put(subMarkPriceStr,MARK_PRICE);
            }
        }
        subDepthAll(subDepthList);
//        subTradeAll(subTradeList);
        log.info("-----盘口-----"+ JSON.toJSONString(subDepthList));// 打印订阅的数据
        log.info("-----成交记录-----"+JSON.toJSONString(subTradeList));// 打印订阅的数据
    }

    private void subDepthAll(List<String> list){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("method","SUBSCRIBE");
        jsonObject.put("params",list);
        jsonObject.put("id",System.currentTimeMillis());
        subMessage(jsonObject.toJSONString());//买一卖一盘 最新价
    }

    private void subTradeAll(List<String> list){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("method","SUBSCRIBE");
        jsonObject.put("params",list);
        jsonObject.put("id",System.currentTimeMillis());
        subMessage(jsonObject.toJSONString());//交易明细
    }

    private void subMessage(String subStr){
        webSocket.send(subStr);
    }

    public void SUBSCRIBE() {
        webSocket = BinanceWebSocket.createConnectionManage(this,api);// 创建websocket连接
        JSONObject jsonObject = new JSONObject();
        ArrayList<String> arr = new ArrayList<>();
        arr.add("btcusdt@miniTicker");
        jsonObject.put("method", "SUBSCRIBE");
        jsonObject.put("params", arr);
        jsonObject.put("id", 1);
        subMessage(jsonObject.toJSONString());
    }

    public void LIST_SUBSCRIPTIONS() {
        webSocket = BinanceWebSocket.createConnectionManage(this,api);// 创建websocket连接
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("method", "LIST_SUBSCRIPTIONS");
        jsonObject.put("id", 3);
        subMessage(jsonObject.toJSONString());
    }

    public void aggTrade() {
        webSocket = BinanceWebSocket.createConnectionManage(this,"/ws/BNBBTC@aggTrade");// 创建websocket连接

    }

}
